package org.lsb.app_back.controller;

import lombok.extern.slf4j.Slf4j;
import org.lsb.app_back.domain.BackendUser;
import org.lsb.app_back.domain.DevUser;
import org.lsb.app_back.dto.UserDTO;
import org.lsb.app_back.service.BackendUserService;
import org.lsb.app_back.service.DevUserUserService;
import org.lsb.app_back.util.JwtUtil;
import org.lsb.app_back.vo.BackendUserVo;
import org.lsb.app_back.vo.DevUserVo;
import org.lsb.app_back.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    BackendUserService backendUserService;
    @Autowired
    DevUserUserService devUserUserService;

    @PostMapping("/login")
    public ResultVo Login(@RequestBody UserDTO userDTO){
        log.info("当前登录信息: {}",userDTO);

        Map<String, Object> claims = new HashMap<>();

           // 根据账户类型调用不同的验证方法
        if ("admin".equals(userDTO.getAccountType())) {
           BackendUser backendUser = backendUserService.authenticateBackendUser(userDTO.getAccount(), userDTO.getPassword());
            if (backendUser!=null) {

                claims.put("id",backendUser.getId());
                claims.put("用户名",backendUser.getUsername());
                claims.put("密码",backendUser.getUserpassword());
                String jwt = JwtUtil.generateJwt(claims);

                BackendUserVo backendUserVo=BackendUserVo.builder()
                        .id(backendUser.getId())
                        .usercode(backendUser.getUsercode())
                        .userpassword(backendUser.getUserpassword())
                        .token(jwt)
                        .build();

                return ResultVo.success("管理员登录成功", backendUserVo);
            } else {
                return ResultVo.error("管理员登录失败：用户名或密码错误");
            }
        } else if ("dev".equals(userDTO.getAccountType())) {
            DevUser devUser = devUserUserService.authenticateDevUser(userDTO.getAccount(), userDTO.getPassword());
            if (devUser!=null) {


                claims.put("id",devUser.getId());
                claims.put("用户名",devUser.getDevname());
                claims.put("密码",devUser.getDevpassword());
                String jwt = JwtUtil.generateJwt(claims);

                DevUserVo devUserVo =DevUserVo.builder()
                        .id(devUser.getId())
                        .devcode(devUser.getDevcode())
                        .devpassword(devUser.getDevpassword())
                        .token(jwt)
                        .build();

                return ResultVo.success("开发者登录成功", devUserVo);
            } else {
                return ResultVo.error("开发者登录失败：用户名或密码错误");
            }

        } else {
            return ResultVo.error("无法识别账户类型");
        }
    }
  }

